#include <stdio.h>
#include <stdlib.h>	/* exit() */
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <limits.h>
#include <errno.h>


/*
 * 线程安全的 getenv 的兼容版本
 */


#define ARG_MAX 1024

extern char **environ;

static pthread_key_t key;
static pthread_mutex_t env_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t init_done = PTHREAD_ONCE_INIT;

static void thread_init(void)
{
	pthread_key_create(&key, free);
}

char * getenv(const char * name)
{
	int i, len;
	char * envbuf;

	pthread_once(&init_done, thread_init);
	pthread_mutex_lock(&env_mutex);

	envbuf = (char *) pthread_getspecific(key);
	if (envbuf == NULL) {
		envbuf = malloc(ARG_MAX);
		if (envbuf == NULL) {
			pthread_mutex_unlock(&env_mutex);
			return NULL;
		}
	}

	len = strlen(name);

	for (i = 0; environ[i] != NULL; i++) {
		if (( strncmp(name, environ[i], len) == 0) && 
			( environ[i][len] == '=')) {
			strcpy(envbuf, &environ[i][len + 1]);
			pthread_mutex_unlock(&env_mutex);

			return envbuf;
		}
	}

	pthread_mutex_unlock(&env_mutex);

	return NULL;
}


int main(void)
{
	
	

	exit(0);
}


